/**
 * 
 */
package com.jxc.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jxc.dao.RemindWarnDaoI;
import com.jxc.model.po.CustomerType;
import com.jxc.model.po.RemindWarn;
import com.jxc.model.po.MemberType;
import com.jxc.model.po.RemindWarn;
import com.jxc.model.po.User;
import com.jxc.model.vo.VRemindWarn;
import com.jxc.model.vo.VRemindWarn;

/**
 * @author Tino
 * 
 */
@Repository("threadRemindWarnDao")
public class RemindWarnDaoImpl implements RemindWarnDaoI {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Serializable save(RemindWarn remindWarn) {
		Session session = this.sessionFactory.openSession();
		Serializable serial = session.save(remindWarn);
		session.close();
		return serial;
	}
	
	@Override
	public void delete(RemindWarn remindWarn) {
		Session session = this.sessionFactory.openSession();
		session.beginTransaction();
		session.delete(remindWarn);
		session.getTransaction().commit();
		session.close();
	}
	
	@Override
	public List<VRemindWarn> combobox(VRemindWarn vRemindWarn) {
		Session session = this.sessionFactory.openSession();
		List<VRemindWarn> list = this.changeModelRemindWarn(this.find(vRemindWarn,session));
		session.close();
		return list;
	}

	private List<RemindWarn> find(VRemindWarn vRemindWarn,Session session) {
		String hql = "from RemindWarn t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vRemindWarn, hql, values);

		Query q = session.createQuery(hql);
		if (values != null && values.size() > 0) {
			for (int i = 0; i < values.size(); i++) {
				q.setParameter(i, values.get(i));
			}
		}

		return q.list();
	}

	private List<VRemindWarn> changeModelRemindWarn(List<RemindWarn> remindWarns) {
		List<VRemindWarn> vRemindWarns = new ArrayList<VRemindWarn>();
		if (remindWarns != null && remindWarns.size() > 0) {
			for (RemindWarn r : remindWarns) {
				VRemindWarn vRemindWarn = new VRemindWarn();
				BeanUtils.copyProperties(r, vRemindWarn);
				vRemindWarns.add(vRemindWarn);
			}
		}
		return vRemindWarns;
	}

	private String addWhere(VRemindWarn vRemindWarn, String hql, List<Object> values) {
		if (vRemindWarn.getBeginDate() != null) {
			hql += " and t.beginDate=? ";
			values.add(vRemindWarn.getBeginDate());
		}
		if (vRemindWarn.getType() != null) {
			hql += " and t.type = ? ";
			values.add(vRemindWarn.getType());
		}
		return hql;
	}
	
}
